<?php
namespace Api\Controller;
use Think\Controller;
use Think\Vender;
use Think\Model;
class GetopenidController extends WxtokenController{
	public function __construct(){
		parent::__construct();
		session_start();
	}

	public function index(){
		//微信服务器根据设置的URL GET过来数据，这里接受判断是否有加密数据
		if (!isset($_GET['echostr'])) {
		    $this->responseMsg();
		}else{
		   $this->checkSignature();
		}	
	}

	//验证服务器url
	public function checkSignature(){
	  $signature=$_GET["signature"];
		$timestamp=$_GET["timestamp"];
		$nonce=$_GET["nonce"];
		$token='xCqf7701W6KHpKesAWpAP7nEq0zwcebF';    //填写的token同微信平台填写的要相同
		if(Validation($signature,$timestamp,$nonce,$token)){ //调用common里面的验证服务器url方法
		 	$echoStr = $_GET["echostr"];  //如果验证成功 把加密字段原封不动返回去
            echo $echoStr;//校验成功，返回随机字符串   
            exit; 		  //注意：在配置URL完验证的时候 只做一次验证所以程序最后exit
        }
	}

	//验证服务器消息
	public function responseMsg(){
        //接收的数据传递方式：get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        //extract post data
        if (!empty($postStr)){
                //解析数据
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                //发送消息方ID
                $fromUsername = $postObj->FromUserName;
                //接收消息方ID
                $toUsername = $postObj->ToUserName;
                //消息类型
                $form_MsgType = $postObj->MsgType;
                $keyword = trim($postObj->Content);
                $time = time();
                //文本消息
                $textTpl = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            <FuncFlag>0</FuncFlag>
                            </xml>";   
                //图片消息
                $newsTpl = "<xml>  
                           <ToUserName><![CDATA[%s]]></ToUserName>  
                           <FromUserName><![CDATA[%s]]></FromUserName>  
                           <CreateTime>%s</CreateTime>  
                           <MsgType><![CDATA[%s]]></MsgType>  
                           <ArticleCount>%s</ArticleCount>  
                           <Articles>  
                           <item>  
                           <Title><![CDATA[%s]]></Title>   
                           <Description><![CDATA[%s]]></Description>  
                           <PicUrl><![CDATA[%s]]></PicUrl>  
                           <Url><![CDATA[%s]]></Url>  
                           </item>  
                           </Articles>  
                           <FuncFlag>1</FuncFlag>  
                           </xml> ";  

                    //事件消息
                  if($form_MsgType=="event"){
                    //获取事件类型
                    $form_Event = $postObj->Event;
                    //订阅事件
                    if($form_Event=="subscribe"){
                      $wx_token=$this->get_token();
                      //回复欢迎文字消息
                      $msgType = "text";
                      $contentStr = "感谢您关注诗宇科技的微信公众号[玫瑰]";
                      $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $contentStr);
                      $this->get_wxuserinfo($wx_token,$fromUsername);
                      echo $resultStr;
                      exit;
                    }    
                  }
          //如果用户发送内容不为空，回复“谢谢您的回复!”    
                 if(!empty( $keyword ) && $keyword=="公司地址"){
                 	$wx_token=$this->get_token();
                    $msgType = "text";
                    //$contentStr = $wx_token."+".$fromUsername;
                    $contentStr = "乌鲁木齐天津南路682号创业大厦1108";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    $this->get_wxuserinfo($wx_token,$fromUsername);
                    echo $resultStr;

                }else{
                    echo "Input something...";
                }

        }else {
            echo "";
            exit;
        }
    }
}